<script setup>
import {onMounted, reactive, ref} from 'vue';
import axios from "axios";
import server from "../../config/server.js";
import {message} from "ant-design-vue";
import {useRouter} from "vue-router";

const router = useRouter()
const students = ref([])
const currentStudentId = ref()
const formState = ref({});
const onFinish = async (values) => {
  console.log('Success:', values, formState);
  if (!currentStudentId.value) {
    message.warning("学生不能为空")
    return
  }

  console.log("currentStudentId", currentStudentId.value);
  console.log("name", getStudentName(currentStudentId.value));
  const countNum = await getStudentCourseCount()
  console.log("courseCount", countNum)
  if (!countNum || !countNum.num) {
    message.warning("该学生没有可用课时")
    return
  }
  const data = {
    student_id: currentStudentId.value,
    name: getStudentName(currentStudentId.value),
    course_start_time: formState.value.course_start_time,
    course_end_time: formState.value.course_end_time,
    left_course: countNum.num - 1,
  }
  if (!data.course_start_time || !data.course_end_time) {
    message.error("开始时间或结束时间不能为空")
    return
  }
  console.log("data", data)
  await saveStudentRecord(data)
  await updateStudentCourseCount(countNum.id, countNum.num)
  router.push("/student/record_list")
};

const updateStudentCourseCount = async (id, count) => {
  axios({
    method: "put",
    contentType: "application/json",
    url: `${server.apiBaseUrl}/zdppy_amcountdown_num/${id}`,
    data: {
      num: count - 1
    }
  }).then(() => {
    message.success(("修改课时数量成功"))
  })
}

const saveStudentRecord = async (data) => {
  await axios({
    method: "post",
    contentType: "application/json",
    url: `${server.apiBaseUrl}/zdppy_amstudent_record`,
    data,
  }).then(() => {
    message.success("新增学生上课记录成功")
  })
}

const loadStudentData = () => {
  axios({
    method: "get",
    url: `${server.apiBaseUrl}/zdppy_amstudent_student?size=10000`,
  }).then(resp => {
    if (resp.data && resp.data.data && resp.data.data.results) {
      students.value = resp.data.data.results
      console.log(" students.value", students.value)
    }
  })
}

const getStudentName = (id) => {
  for (let student of students.value) {
    if (student.id === id) {
      return student.name
    }
  }
}
const getStudentCourseCount = async () => {
  let countNum = null
  await axios({
    method: "get",
    url: `${server.apiBaseUrl}/zdppy_amcountdown_num?pk_id=${currentStudentId.value}`,
  }).then(resp => {
    countNum = resp.data.data.results[0]
  })
  return countNum
}

onMounted(() => {
  loadStudentData()
})

</script>
<template>
  <a-form
      :model="formState"
      @finish="onFinish"
  >
    <a-form-item
        name="name"
        label="学生"
    >
      <a-select
          v-model:value="currentStudentId"
      >
        <a-select-option
            v-for="(v,k) in students"
            :key="v.id"
            :value="v.id"
        >
          {{ v.name }}
        </a-select-option>
      </a-select>
    </a-form-item>
    <a-form-item
        name="course_start_time"
        label="开始时间"
        :rules="[{ required: true, message: '开始时间不能为空' }]"
    >
      <a-date-picker
          v-model:value="formState.course_start_time"
          show-time
          format="YYYY-MM-DD HH:mm:ss"
          value-format="YYYY-MM-DD HH:mm:ss"
      />
    </a-form-item>
    <a-form-item
        name="course_end_time"
        label="结束时间"
        :rules="[{ required: true, message: '结束时间不能为空' }]"
    >
      <a-date-picker
          v-model:value="formState.course_end_time"
          show-time
          format="YYYY-MM-DD HH:mm:ss"
          value-format="YYYY-MM-DD HH:mm:ss"
      />
    </a-form-item>
    <a-form-item>
      <a-button type="primary" html-type="submit">提交</a-button>
    </a-form-item>
  </a-form>
</template>